<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Examples;
use App\Http\Controllers\Controller;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class HomeController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('Dashboard')
            ->description('Description...')
            ->body(function (Row $row) {
                $row->column(6, function (Column $column) {
                    $column->row(Dashboard::title());
                    $column->row(new Examples\Tickets());
                });

                $row->column(6, function (Column $column) {
                    $column->row(function (Row $row) {
                        $row->column(6, new Examples\NewUsers());
                        $row->column(6, new Examples\NewDevices());
                    });

                    $column->row(new Examples\Sessions());
                    $column->row(new Examples\ProductOrders());
                });
            });
    }


    public function uploadTinymc(Request $request){
        $img= $request->input('image');

        //获取图片的扩展名
        $filename = basename($img);
        //获取文件的绝对路径
        $client = new Client([
            'verify' => false
        ]);
        
        if (strpos($img, "editor") === false) {
            // 存储图片到指定目录
            $response = $client->get($img);
            $savePath = public_path("uploads/editor/images/".date('Y-m-d')."/". $filename);
            $folderPath = "uploads/editor/images/".date('Y-m-d');
            // 确保目录存在，如果不存在则创建
            if (!is_dir($folderPath)) {
                mkdir($folderPath, 0755, true);
            }
            Storage::disk('admin')->put($filename,file_put_contents($savePath, $response->getBody()));
            $filename = "/".$folderPath."/". $filename;
        } else {
            $filename = $img;
        }
        $res = [
            'data'=>$filename,
            'code'=>200
        ];
        return response()->json($res);
    }
}
